﻿{include file="/public/header" /}
<div class="content-wraper" style="width: 100%;">
    <div class="panel mg10" role="center-part">
        <div class="back-breadcrumb">
            <a class="back-link-icon" href="javascript:history.back(-1);"><i class="iconfont iconRectangleCopy10"></i>返回上一页</a>
            &nbsp;&nbsp;/&nbsp;&nbsp;当前位置：门店角色{if isset($res['id']) AND $res['id']}设置{else}添加{/if}
        </div>
        <div class="wrap-title">
            <h4>门店角色{if isset($res['id']) AND $res['id']}设置{else}添加{/if}</h4>
            <div class="normal-line"></div>
        </div>
        <form class="normal_form" method="post" onsubmit="return Core.Form(this);" style="margin-top: 20px">
            <div class="row">
                <div class="field_text">组名称：</div>
                <input type="text" class="input-text w300" name="title" value="{$res['title'] ?? ''}" maxlength="30" />
            </div>
            <div class="row">
                <div class="field_text">标识码：</div>
                <input type="text" class="input-text w300" name="gkey" value="{$res['gkey'] ?? ''}" maxlength="20" /> <span style="color: gray;">2~20字符内，支持数字、字母、点中下划线</span>
            </div>
            
            <div class="row">
                {if isset($Auth) AND is_array($Auth)}
                {foreach $Auth as $mkey => $items}
                <!--大分类-->
                <div class="group-wraper">
                    <div class="field_text"><span>{$items['title'] ?? ''}</span><i class="g-ico-toright"></i></div>
                    <!--一级产品-->
                    {if isset($items['auth']) AND is_array($items['auth'])}
                        {foreach $items['auth'] as $akey => $auth}
                        <div class="group-content">
                        <div class="group-assort-title" attr-type="{$mkey ?? ''}-{$akey ?? ''}">
                            <label><span>{$auth ?? ''}</span><input class="ck_group" type="checkbox" name="settings[{$mkey ?? ''}][]" value="{$akey ?? ''}" {if $akey && ($res['settings'][$mkey] ?? NULL) AND in_array($akey,$res['settings'][$mkey])}checked{/if}></label>                                
                        </div>
                        </div>
                        {/foreach}
                    {/if}
                        
                </div>
                {/foreach}
                {/if}
            
            </div>
            <div class="row" style="margin: 35px 0;">
                <div class="field_text">&nbsp;</div>
                <button type="reset" class="btn btn-cancel large">重置</button>
                <button class="btn btn-primary large" type="submit" style="margin-left: 15px;">保存</button>
            </div>
        </form>
    </div>
</div>
<script type="text/javascript">
Core.Form = function(form){
    /**表单提交 */
    var form = $(form);
    form.find('p.colortip').remove();

    var p = {
        'title' : $.trim(form.find(':text[name="title"]').val()),
        'gkey'  : $.trim(form.find(':text[name="gkey"]').val())
    }
    if (!Core.rule('*',p['title'])) {
		Core.Dialog.msg("请输入角色名称");return false;
	} else if (p['gkey']!='' && !Core.rule('key',p['gkey'])) {
		Core.Dialog.msg("请设置正确的标识码");return false;
	}

    p.checklist = {};
    form.find('input[type="checkbox"]:checked').each(function(){
        var _t = $(this);
        var _v = _t.val();
        var _n = _t.attr('name');
        if(typeof p.checklist[_n] === 'undefined'){
            p.checklist[_n] = [];
        }
        p.checklist[_n].push(_v);
    });
    return Core.submit();
}

Core.CheckGroup = {
    defaults: {},
    init: function($box){
        this.defaults.box = $box;
        var _this = this;
        /**点击箭头收起打开模块组 */
        $box.find('.ic_down_arrow').on('click',function(){
            var _t = $(this);
            _t.toggleClass('close');
            _t.parents('.group-content').find('.group-assort-content').each(function(idx,itm){
                $(itm).slideToggle();
            });
        });
        _this.setWraper('.group-content','.group-assort-content');
        _this.setItemCheck('.ck_item');
        _this.setGroupCheck('.ck_group');
        
    },
    setGroupCheck: function(ckgroup){
        /**全选/取消选中模块组 */
        var _this = this;
        _this.defaults.box.find(ckgroup).on('change', function(e){
            var _t = $(this);
            var _isChecked = _t.prop("checked");
            var _parentc = _t.parents('.group-content');
            _isChecked && _parentc.find('.group-base-list').length>0? 
                _parentc.find('.group-base-list').addClass('base-checked-all'):
                _parentc.find('.group-base-list').removeClass('base-checked-all');
            _parentc.find(".ck_item").each(function(idx, itm){
                $(itm).prop("checked", _isChecked);
            });
        })
    },
    setItemCheck: function(ck){
        var _this = this;
        var _base = '.group-base-list';
        /**切换选中效果 */
        _this.defaults.box.find(ck).on('change', function(e){
            var _t = $(this);
            var _v = _t.val();
            var _isChecked = _t.prop("checked");
            var _parentc = _t.parents('.group-content');

            var _dk = _t.attr('data-key');
            if(typeof _dk!="undefined" && $(_base+"[data-key="+_dk+"]").length>0){
                _isChecked? _this.baseChecked(true, _base, _dk): _this.baseChecked(false, _base, _dk);
            }
            if(_t.parents(_base).length>0){
                _dk = _t.parents(_base).attr('data-key');
                $(ck+"[data-key="+_dk+"]").prop("checked", _t.parents(_base).find(ck+":checked").length>0);
                _t.parents(_base).find(ck+":checked").length>0? 
                    _t.parents(_base).addClass('base-checked-all'):
                    _t.parents(_base).removeClass('base-checked-all');
            }
            _parentc.find(".ck_item:checked").length>0?
                _parentc.find(".ck_group").prop("checked", true):
                _parentc.find(".ck_group").prop("checked", false);
        });
    },
    baseChecked: function(isChecked, _basestr, _dk_val){
        //查看其他人的线索被选中时
        var $_basestr = $(_basestr+"[data-key="+_dk_val+"]");
        $_basestr.find(".ck_item").each(function(idx, itm){
            $(itm).prop("checked", isChecked);
            isChecked? $_basestr.addClass('base-checked-all'): $_basestr.removeClass('base-checked-all');
        });
    },
    setWraper: function(str2, str3){
        /**判断模块子选项 */
        this.defaults.box.each(function(index,warp){
            var isHasChildren = false;
            var _box_2 = $(warp).find(str2);
            if(typeof _box_2 === "undefined" || _box_2.length === 0) {
                return false;
            }
            _box_2.each(function(index2, warp2){
                var _box_3 = $(warp2).find(str3);
                if(_box_3.length>0){
                    isHasChildren = true;
                }
                if(!isHasChildren && index2 === _box_2.length-1) {
                    $(warp).addClass("none-children");
                }
            })
        })
    }
}
$(function(){
    /**初始化 */
    Core.CheckGroup.init($('.group-wraper'));
});
</script>
{include file="/public/footer" /}